{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import logging\n",
    "logging.basicConfig(level=logging.ERROR)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sepal length (cm)</th>\n",
       "      <th>sepal width (cm)</th>\n",
       "      <th>petal length (cm)</th>\n",
       "      <th>petal width (cm)</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5.1</td>\n",
       "      <td>3.5</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4.9</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>4.7</td>\n",
       "      <td>3.2</td>\n",
       "      <td>1.3</td>\n",
       "      <td>0.2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   sepal length (cm)  sepal width (cm)  petal length (cm)  petal width (cm)\n",
       "0                5.1               3.5                1.4               0.2\n",
       "1                4.9               3.0                1.4               0.2\n",
       "2                4.7               3.2                1.3               0.2"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.datasets import load_iris\n",
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "X, y = load_iris(return_X_y=True, as_frame=True)\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n",
    "X[:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    0\n",
       "1    0\n",
       "2    0\n",
       "Name: target, dtype: int64"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y[:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "17:28:10 I hypernets.m.hyper_model.py 249 - 3 class detected, inferred as a [multiclass classification] task\n",
      "17:28:10 I hypernets.c.meta_learner.py 22 - Initialize Meta Learner: dataset_id:74e7c134740a0f846f4c5e57fa5e6c93\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>trial No.</th>\n",
       "      <th>Previous reward</th>\n",
       "      <th>Best trial</th>\n",
       "      <th>Best reward</th>\n",
       "      <th>Total elapsed</th>\n",
       "      <th>Max trials</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.869721</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   trial No.  Previous reward  Best trial  Best reward  Total elapsed  \\\n",
       "0         10              1.0           3          1.0       1.869721   \n",
       "\n",
       "   Max trials  \n",
       "0          10  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/markdown": [
       "#### Current Trial:"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Trial No.</th>\n",
       "      <th>Reward</th>\n",
       "      <th>Elapsed</th>\n",
       "      <th>Space Vector</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>3</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.219028</td>\n",
       "      <td>[2, 3, 1, 0, 0, 4, 1]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.189763</td>\n",
       "      <td>[0, 0, 1, 3, 2, 1, 1, 0, 3, 0, 2]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>5</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.167851</td>\n",
       "      <td>[1, 0, 1, 1, 135, 2, 0, 4, 1, 1]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>6</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.244890</td>\n",
       "      <td>[1, 2, 0, 1, 380, 3, 0, 2, 0]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>8</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.213203</td>\n",
       "      <td>[2, 1, 1, 3, 0, 4, 0]</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Trial No.  Reward   Elapsed                       Space Vector\n",
       "0          3     1.0  0.219028              [2, 3, 1, 0, 0, 4, 1]\n",
       "1          4     1.0  0.189763  [0, 0, 1, 3, 2, 1, 1, 0, 3, 0, 2]\n",
       "2          5     1.0  0.167851   [1, 0, 1, 1, 135, 2, 0, 4, 1, 1]\n",
       "3          6     1.0  0.244890      [1, 2, 0, 1, 380, 3, 0, 2, 0]\n",
       "4          8     1.0  0.213203              [2, 1, 1, 3, 0, 4, 0]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "categorical_feature in Dataset is overridden.\n",
      "New categorical_feature is []\n"
     ]
    },
    {
     "data": {
      "text/markdown": [
       "#### Best Trial:"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<table border=\"1\" class=\"dataframe\">\n",
       "<thead>\n",
       "<tr style=\"text-align: right;\">\n",
       "  <th>key</th>\n",
       "  <th>value</th>\n",
       "</tr>\n",
       "</thead>\n",
       "<tbody><tr>\n",
       "  <td>signature</td>\n",
       "  <td>da05dc439d71de68603898e20da223de</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <td>vectors</td>\n",
       "  <td>[2, 3, 1, 0, 0, 4, 1]</td>\n",
       "</tr><tr>\n",
       "  <td>0-estimator_options.hp_or</td>\n",
       "  <td>2</td>\n",
       "</tr>\n",
       "<tr><tr>\n",
       "  <td>1-numeric_imputer_0.strategy</td>\n",
       "  <td>most_frequent</td>\n",
       "</tr>\n",
       "<tr><tr>\n",
       "  <td>2-numeric_scaler_optional_0.hp_opt</td>\n",
       "  <td>True</td>\n",
       "</tr>\n",
       "<tr><tr>\n",
       "  <td>3-Module_CatBoostEstimator_1.learning_rate</td>\n",
       "  <td>0.001</td>\n",
       "</tr>\n",
       "<tr><tr>\n",
       "  <td>4-Module_CatBoostEstimator_1.depth</td>\n",
       "  <td>3</td>\n",
       "</tr>\n",
       "<tr><tr>\n",
       "  <td>5-Module_CatBoostEstimator_1.l2_leaf_reg</td>\n",
       "  <td>30</td>\n",
       "</tr>\n",
       "<tr><tr>\n",
       "  <td>6-numeric_or_scaler_0.hp_or</td>\n",
       "  <td>1</td>\n",
       "</tr>\n",
       "<tr>  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "DAG_HyperSpace_1"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "17:28:11 I hypernets.d.in_process_dispatcher.py 119 - Trial 1 done, reward: 0.9333333333333333, best_trial_no:1, best_reward:0.9333333333333333\n",
      "\n",
      "17:28:11 I hypernets.d.in_process_dispatcher.py 119 - Trial 2 done, reward: 0.9666666666666667, best_trial_no:2, best_reward:0.9666666666666667\n",
      "\n",
      "17:28:11 I hypernets.d.in_process_dispatcher.py 119 - Trial 3 done, reward: 1.0, best_trial_no:3, best_reward:1.0\n",
      "\n",
      "17:28:11 I hypernets.d.in_process_dispatcher.py 119 - Trial 4 done, reward: 1.0, best_trial_no:3, best_reward:1.0\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "categorical_feature in Dataset is overridden.\n",
      "New categorical_feature is []\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "17:28:11 I hypernets.d.in_process_dispatcher.py 119 - Trial 5 done, reward: 1.0, best_trial_no:3, best_reward:1.0\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "categorical_feature in Dataset is overridden.\n",
      "New categorical_feature is []\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "17:28:12 I hypernets.d.in_process_dispatcher.py 119 - Trial 6 done, reward: 1.0, best_trial_no:3, best_reward:1.0\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "categorical_feature in Dataset is overridden.\n",
      "New categorical_feature is []\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "17:28:12 I hypernets.d.in_process_dispatcher.py 119 - Trial 7 done, reward: 0.3, best_trial_no:3, best_reward:1.0\n",
      "\n",
      "17:28:12 I hypernets.d.in_process_dispatcher.py 119 - Trial 8 done, reward: 1.0, best_trial_no:3, best_reward:1.0\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "categorical_feature in Dataset is overridden.\n",
      "New categorical_feature is []\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "17:28:12 I hypernets.d.in_process_dispatcher.py 119 - Trial 9 done, reward: 1.0, best_trial_no:3, best_reward:1.0\n",
      "\n",
      "17:28:13 I hypernets.d.in_process_dispatcher.py 119 - Trial 10 done, reward: 1.0, best_trial_no:3, best_reward:1.0\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from hypergbm.search_space import search_space_general\n",
    "from hypergbm import HyperGBM\n",
    "from hypernets.searchers.evolution_searcher import EvolutionSearcher\n",
    "\n",
    "rs = EvolutionSearcher(search_space_general,  200, 100, optimize_direction='max')\n",
    "\n",
    "hk = HyperGBM(rs, task='multiclass', reward_metric='accuracy', callbacks=[])\n",
    "\n",
    "hk.search(X_train, y_train, X_eval=X_test, y_eval=y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "estimator = hk.load_estimator(hk.get_best_trial().model_file)\n",
    "y_pred = estimator.predict(X_test)\n",
    "\n",
    "from sklearn.metrics import accuracy_score\n",
    "accuracy_score(y_test, y_pred)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
